package net.bwie.realtime.vehicle.dwd.job;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import net.bwie.realtime.utils.KafkaUtil;
import net.bwie.realtime.vehicle.dwd.utils.MonitorUtil;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;


/**
 * @author ASUS
 */
public class OverSpeedController {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        DataStream<String> dwdCarMonitor = KafkaUtil.consumerKafka(env, "dwd_car_monitor");

//        dwdCarMonitor.print();
        DataStream<String> ds1 = processJson(dwdCarMonitor);
        ds1.print();

        env.execute("OverSpeedController");
    }

    private static DataStream<String> processJson(DataStream<String> dwdCarMonitor) {
        DataStream<String> map = dwdCarMonitor.map(new MapFunction<String, String>() {
            @Override
            public String map(String s) throws Exception {
                JSONObject jsonObject = JSON.parseObject(s);
                jsonObject.put("speedLimit", jsonObject.getInteger("speedLimit"));
                return jsonObject.toJSONString();
            }
        });
        MonitorUtil.preloadAreaInfo();
        return map;
    }
}
